import deviceAPI from "@/api/device/device";
export default {
  props: {
    device: {
      type: Object,
      default() {
        return {
          message: {},
        };
      },
    },
    showWhole: {
      type: Boolean,
      default: false,
    },
    showList: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      commanding: false,
      all: false,
      map: {},
    };
  },
  computed: {
    list() {
      const list = [];
      for (var k in this.map) {
        list.push(this.map[k]);
      }
      return list;
    },
  },
  watch: {
    device(nv) {
      this.addDevice(nv);
    },
  },
  created() {
    this.addDevice(this.device);
  },
  methods: {
    addDevice(nv) {
      if (!nv || !nv.devMac) {
        return;
      }
      const obj = {};
      obj[nv.devMac] = nv;
      this.map = Object.assign({}, this.map, obj);
    },
    deviceCommand(command, content, action) {
      this.commanding = true;
      const { code } = this.device;
      deviceAPI
        .command({
          code,
          command,
          content,
          action,
        })
        .then(() => {
          this.commanding = false;
        });
    },
  },
};
